import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('../layouts/MainLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('../views/Home.vue'),
        meta: { exactPath: true }
      },
      {
        path: 'work-items',
        name: 'WorkItems',
        component: () => import('../views/work-item/List.vue')
      },
      {
        path: 'employees',
        name: 'Employees',
        component: () => import('../views/employee/List.vue')
      },
      {
        path: 'employees/:id',
        name: 'EmployeeDetail',
        component: () => import('../views/employee/Detail.vue'),
        props: true
      },
      {
        path: 'other',
        name: 'Other',
        component: () => import('../views/Other.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth !== false)

  if (requiresAuth && !token) {
    next('/login')
  } else if (to.path === '/login' && token) {
    next('/')
  } else {
    next()
  }
})

export default router 